#region License

// This file is part of Grawlix.
// 
// Grawlix is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Grawlix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with Grawlix.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using Castle.DynamicProxy;
using Grawlix.Services;

namespace Grawlix
{
	/// <summary>
	/// Methods for creating permanent dynamic proxies.
	/// </summary>
	public static class DynamicProxy
	{
		/// <summary>
		/// Creates a proxy to the specified target.
		/// </summary>
		/// <typeparam name="T">The type to proxy as.</typeparam>
		/// <param name="target">The target to proxy.</param>
		/// <param name="baseClass">The base class to proxy.</param>
		/// <returns>The proxy.</returns>
		public static T Create<T>(T target, Type baseClass)
		{
			var generator = new ProxyGenerator();
			var options = new ProxyGenerationOptions
			{
				BaseTypeForInterfaceProxy = baseClass
			};
			return generator.CreateInterfaceProxyWithTarget<T>(
				target, options);
		}

		/// <summary>
		/// Creates a proxy to the specified target.
		/// </summary>
		/// <typeparam name="T">The type to proxy as.</typeparam>
		/// <param name="target">The target to proxy.</param>
		/// <returns>The proxy.</returns>
		public static T Create<T>(T target)
		{
			var generator = new ProxyGenerator();
			return generator.CreateInterfaceProxyWithTarget<T>(
				target);
		}

		/// <summary>
		/// Creates a permanent proxy to the specified target.
		/// </summary>
		/// <typeparam name="T">The type to proxy as.</typeparam>
		/// <param name="target">The target to proxy.</param>
		/// <returns>The proxy.</returns>
		public static T CreatePermanent<T>(T target)
		{
			return Create(target, typeof(PermanentMarshalByRefObject));
		}
	}
}